package fr.labri.macchiato.framework.technical.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A transient repository, that simply stores the entities into a
 * {@link HashMap}, so they will not be persisted when the application stops
 * 
 * @author Matthieu FOUCAULT
 * 
 * @param <T>
 * @param <Tid>
 */
public class TransientRepository<T extends Identifiable<Tid>, Tid> implements
		Repository<T, Tid> {

	private Map<Tid, T> repository;

	/**
	 * Default constructor
	 */
	public TransientRepository() {
		repository = new HashMap<Tid, T>();
	}

	@Override
	public void save(T entity) {
		repository.put(entity.getId(), entity);

	}

	@Override
	public T load(Tid id) {
		return repository.get(id);
	}

	@Override
	public List<T> loadAll() {
		return new ArrayList<T>(repository.values());
	}

	@Override
	public void remove(Tid id) {
		repository.remove(id);

	}

	@Override
	public void clean() {
		repository.clear();

	}

	@Override
	public boolean isEmpty() {
		return repository.isEmpty();
	}

}
